package com.java110.fee.cmd.cheque;

import com.alibaba.fastjson.JSONObject;
import com.java110.core.annotation.Java110Cmd;
import com.java110.core.context.ICmdDataFlowContext;
import com.java110.core.event.cmd.Cmd;
import com.java110.core.event.cmd.CmdEvent;
import com.java110.core.log.LoggerFactory;
import com.java110.dto.cheque.ChequeDto;
import com.java110.fee.cmd.meterType.ListMeterTypeCmd;
import com.java110.intf.fee.IChequeInnerServiseSMO;
import com.java110.utils.exception.CmdException;
import com.java110.utils.util.BeanConvertUtil;
import com.java110.vo.ResultVo;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * 票据管理，查询
 * 服务编码：cheque.listCheque
 */
@Java110Cmd(serviceCode = "cheque.listCheque")
public class ListChequeCmd extends Cmd {
    private static Logger logger = LoggerFactory.getLogger(ListChequeCmd.class);

    @Autowired
    private IChequeInnerServiseSMO chequeInnerServiseSMOImpl;
    @Override
    public void validate(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
        super.validatePageInfo(reqJson);
    }

    @Override
    public void doCmd(CmdEvent event, ICmdDataFlowContext context, JSONObject reqJson) throws CmdException, ParseException {
        ChequeDto chequeDto = BeanConvertUtil.covertBean(reqJson,ChequeDto.class);
        int count =  chequeInnerServiseSMOImpl.queryChequesCount(chequeDto);
        List<ChequeDto> chequeDtos = null;

        if(count > 0){
            chequeDtos = chequeInnerServiseSMOImpl.queryCheques(chequeDto);
        }else{
            chequeDtos = new ArrayList<>();
        }
        ResultVo resultVo = new ResultVo((int) Math.ceil((double) count / (double) reqJson.getInteger("row")), count, chequeDtos);

        ResponseEntity<String> responseEntity = new ResponseEntity<String>(resultVo.toString(), HttpStatus.OK);

        context.setResponseEntity(responseEntity);
    }
}
